package com.cap.dreamcircle.View;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Action.FocusAction;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.NotifyBean;
import com.cap.dreamcircle.Model.Bean.NotifyEntity;
import com.cap.dreamcircle.Model.IFriendModel;
import com.cap.dreamcircle.Model.IMessageModel;
import com.cap.dreamcircle.Model.Impl.FriendModelImpl;
import com.cap.dreamcircle.Model.Impl.MessageModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.View.Adapter.DreamNotifyAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.Observable;
import rx.functions.Action0;

/**
 * Created by liucxu on 2018/3/20.
 */

public class NotifyActivity extends BaseActivity {

    @BindView(R.id.swipe_refrsh_notify)
    SmartRefreshLayout swipe_refrsh_notify;
    @BindView(R.id.recyclerview_notify)
    RecyclerView recyclerview_notify;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.layout_back)
    LinearLayout layout_back;

    private DreamNotifyAdapter adapter;

    private List<NotifyEntity> sources = new ArrayList<>();
    private IMessageModel messageModel;
    private IFriendModel friendModel;
    private int page_index = 1;
    private boolean is_refresh = true;
    private boolean key_from_notification;

    @Override
    public int getLayoutId() {
        return R.layout.activity_notify;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        tv_title.setText("通知");
        messageModel = new MessageModelImpl();
        friendModel = new FriendModelImpl();
        adapter = new DreamNotifyAdapter(this, sources);
        key_from_notification=getIntent().getBooleanExtra("key_from_notification",false);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setItemPrefetchEnabled(false);
        recyclerview_notify.setLayoutManager(manager);
        recyclerview_notify.setAdapter(adapter);
        swipe_refrsh_notify.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                is_refresh = true;
                swipe_refrsh_notify.setEnableLoadmore(true);
                requestDreamNotify();
            }
        });
        swipe_refrsh_notify.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                is_refresh = false;
                requestDreamNotify();
            }
        });
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(key_from_notification){
                    Intent intent=new Intent(NotifyActivity.this,MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
                App.GetInstance().finishActivity(NotifyActivity.this);
            }
        });
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        is_refresh=true;
        requestDreamNotify();
    }

    public void onEvent(FocusAction action) {
    }

    private void fellowUser(final FocusAction action) {
        Observable<BaseBean> request = null;
        if (action.isFocus()) {
            request = friendModel.fellow(action.getUserId());
        } else {
            request = friendModel.disFellow(action.getUserId());
        }
        request.subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                if (action.isFocus()) {
                    adapter.resetItemFocusStatus(action.getPosition(),action.isFocus());
                } else {
                    adapter.resetItemFocusStatus(action.getPosition(),action.isFocus());
                }
            }

            @Override
            public void onFailure(BaseBean response) {
                ToastUtils.showShortToast(NotifyActivity.this, response.getDesc());
            }
        });
    }

    private void requestDreamNotify() {
        int local_page = page_index;
        if (is_refresh) {
            local_page = 1;
        }
        messageModel.RequestNotifyMessage(local_page).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new CommonSubscriber<NotifyBean>() {
            @Override
            public void onSuccess(NotifyBean response) {
                hideProgress();
                swipe_refrsh_notify.finishRefresh();
                swipe_refrsh_notify.finishLoadmore();
                if ((response.getMsgList() != null) && (response.getMsgList().size() > 0)) {
                    if (is_refresh) {
                        sources.clear();
                        page_index = 1;
                    }
                    sources.addAll(response.getMsgList());
                    adapter.setDatas(sources);
                    adapter.notifyDataSetChanged();
                    page_index++;
                } else {
                    swipe_refrsh_notify.finishLoadmore();
                    swipe_refrsh_notify.setEnableLoadmore(false);
                }
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                swipe_refrsh_notify.finishRefresh();
            }
        });
    }

    @Override
    public void setupData() {

    }
}
